package  org.fantasy.common.db.center;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import  org.fantasy.common.db.QryFactory;
import  org.fantasy.common.db.Query;
import  org.fantasy.common.db.bean.Row;
import  org.fantasy.common.exception.DBException;
import  org.fantasy.common.util.Constants;
import  org.fantasy.common.util.ResultMap;
import  org.fantasy.common.util.ThreadMap;

public abstract class AbstractQryCenter implements QryCenter {

    private static final Log log = LogFactory.getLog(AbstractQryCenter.class);

    /**
     * 存放到REQUEST中的默认初始页
     */
    private static final int PAGE_START_DEFAULT = 1;

    /**
     * 存放到REQUEST中的默认每页数量
     */
    private static final int PAGE_SIZE_DEFAULT = 50;

    protected String queryId;

    protected Map<String, Object> paramMap = new HashMap<String, Object>();

    protected QryFactory qryFactory;

    public AbstractQryCenter(String queryId, Map<String, Object> paramMap) {
        this.queryId = queryId;
        this.paramMap = paramMap;
    }

    public QryCenter setParamMap(Map<String, Object> map) {
        this.paramMap.putAll(map);
        return this;
    }

    public void setQryFactory(QryFactory qryFactory) {
        this.qryFactory = qryFactory;
    }

    /**
     * 取得SQL(由子类去实现)
     */
    public abstract String getSql() throws DBException;

    /**
     * 把SQL保存进REQUEST
     * @param request
     * @param sql
     * @param paramMap
     */
    @SuppressWarnings("all")
    protected void saveRuntimeSql() throws DBException {
        String runtimeSql = getRuntimeSql();
        ThreadMap.put(Constants.RUNTIME_QUERY, runtimeSql);
        ThreadMap.put(Constants.RUNTIME_QUERY_ID, queryId);
    }

    /**
     * 取得Query对象
     * 
     * @return
     * @throws DBException
     */
    public Query getQuery() throws DBException {
        return qryFactory.getQuery(getSql(), paramMap);
    }

    /**
     * 分页查询
     * 
     * @param queryId
     * @param paramMap
     * @return
     * @throws DBException
     */
    public List<Row> qryPageList() throws DBException {
        ResultMap<String, Object> rm = new ResultMap<String, Object>(paramMap);
        int pageStart = rm.getInt(Constants.PAGE_START, PAGE_START_DEFAULT);
        int pageSize = rm.getInt(Constants.PAGE_SIZE, PAGE_SIZE_DEFAULT);
        Query query = getQuery();
        List<Row> list = query.setPage(pageStart, pageSize).list();
        saveRuntimeSql();
        return list;
    }

    /**
     * 不分页查询
     * 
     * @param sqlId
     * @param paramMap
     * @return
     * @throws DBException
     */
    public List<Row> qryList(String sqlId, Map<String, Object> paramMap) throws DBException {
        Query query = getQuery();
        List<Row> list = query.list();
        saveRuntimeSql();
        return list;
    }

    public List<Row> qryList() throws DBException {
        return qryList(queryId, paramMap);
    }

    public int update() throws DBException {
        Query query = getQuery();
        return query.update();
    }

    public String getRuntimeSql() throws DBException {
        return qryFactory.getQuery(getSql(), paramMap).getRuntimeSql();
    }
}
